package cn.jinjimi.app.custom.ui.activity;

import android.view.View;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.controller.CustomerController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.entity.Insurance;
import cn.jinjimi.app.custom.event.InsuranceAddEvent;
import cn.jinjimi.app.custom.event.InsureChangeEvent;
import cn.jinjimi.app.custom.event.InsureListEvent;
import cn.jinjimi.app.custom.ui.adapter.InsureListAdapter;
import cn.jinjimi.app.custom.ui.dialog.AddInsureDialog;
import cn.jinjimi.app.interfaces.DataSetChangeListener;
import cn.jinjimi.app.main.dialog.LoadingDialog;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;
import cn.jinjimi.app.main.widget.XListView.XListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class InsureListActivity extends ToolBarCommonActivity implements XListView.IXListViewListener, DataSetChangeListener {

    private XListView mListView;
    private InsureListAdapter mAdapter;

    private Custom mCustom;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_insure_list);
        mCustom = (Custom) getIntent().getSerializableExtra(BundleKey.PARAM_CUSTOM);
        setTitle(R.string.insure_list);
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        mListView = (XListView) findViewById(R.id.listview);
        mListView.setPullLoadEnable(false);
        mListView.setTransparentBg();
        mAdapter = new InsureListAdapter(InsureListActivity.this, this);
        mListView.setAdapter(mAdapter);
    }

    protected void initListener() {
        mListView.setXListViewListener(this);
        findViewById(R.id.ll_add_new).setOnClickListener(this);
    }

    protected void initData() {
        LoadingDialog.show(InsureListActivity.this, getString(R.string.loading_data));
        CustomerController.getInsureList(mCustom.customerId);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_add_new:
                showAddInsureDialog();
                break;
        }
    }

    @EventTrace(EventID.APP_CUSTOMER_DETAIL_SUPERLIST_ADD)
    private void showAddInsureDialog() {
        new AddInsureDialog(this, mCustom.customerId).show();
    }

    /* 监听保单的新增结果 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onInsuranceAddEvent(InsuranceAddEvent event) {
        LoadingDialog.dispose();
        switch (event.state) {
            case InsuranceAddEvent.SUCC:
                Insurance insurance = event.data;
                mAdapter.addItem(insurance);
                InsureChangeEvent icevent = new InsureChangeEvent();
                icevent.action = InsureChangeEvent.ACTION_ADD;
                icevent.insure = event.data;
                EventBus.getDefault().post(icevent);
                ToastUtils.toastMessage(this, getString(R.string.add_insurance_succ));
                break;
            case InsuranceAddEvent.FAIL:
                ToastUtils.toastMessage(this, getString(R.string.add_insurance_fail));
                break;
        }
    }

    /* 监听获取保单的结果 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onInsureListEvent(InsureListEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                mAdapter.setItems(event.data.getPolicys());
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(InsureListActivity.this, getString(R.string.insure_get_info_fail));
                dataChange();
                break;
        }
        mListView.setRefreshTime(System.currentTimeMillis());
        mListView.stopRefresh();
        LoadingDialog.dispose();
    }

    /**
     * 监听保单数据的变化:删除,修改等
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onInsureChangeEvent(InsureChangeEvent event) {
        if (event.action == InsureChangeEvent.ACTION_ADD) {
            onRefresh();
            return;
        }
        Insurance newI = event.insure;
        if (newI == null || mAdapter.getCount() == 0)
            return;
        int len = mAdapter.getCount();
        for (int i = 0; i < len; i++) {
            Insurance bean = mAdapter.getItem(i);
            if (bean.getCustomer_id() == newI.getCustomer_id() && bean.getId() == newI.getId()) {
                mAdapter.getItems().remove(i);
                if (event.action == InsureChangeEvent.ACTION_UPDATE) {
                    mAdapter.getItems().add(i, newI);
                }
                break;
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh() {
        CustomerController.getInsureList(mCustom.customerId);
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void dataChange() {
        findViewById(R.id.empty_view).setVisibility(mAdapter.isEmpty() ? View.VISIBLE : View.GONE);
    }

    /* 开始无保单时的动画效果 */
//    private void beginAnim() {
//        AnimationSet animationSet = new AnimationSet(false);
//
//        AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
//        alphaAnimation.setDuration(1000);
//        alphaAnimation.setRepeatMode(Animation.RESTART);
//        alphaAnimation.setRepeatCount(Animation.INFINITE);
//        alphaAnimation.setInterpolator(new AccelerateInterpolator());
//
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 4f, 1f, 4f, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F);
//        scaleAnimation.setDuration(1000);
//        scaleAnimation.setRepeatMode(Animation.RESTART);
//        scaleAnimation.setRepeatCount(Animation.INFINITE);
//
//        animationSet.addAnimation(alphaAnimation);
//        animationSet.addAnimation(scaleAnimation);
//        imgAnim.startAnimation(animationSet);
//    }

//    private void endAnim() {
//        imgAnim.clearAnimation();
//    }
}